package com.ntanh.mealplanner.main.objects;

import android.content.ContentValues;
import android.util.Log;

import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.base.model.NamedEntity;
import com.ntanh.mealplanner.main.dao.DataBaseHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by Anh Nguyen on 2/24/2015.
 */
public class Recipe extends NamedEntity {

    public static final String SOURCE_URL = "sourceUrl";

    public static final String INGREDIENT_LIST = "ingredientList";

    private Map<Ingredient, Float> ingredientList;

    private String sourceURL;

    public Recipe(String author, String name, String image, String description, Map<Ingredient, Float> ingredientList, String sourceURL) {
        super(author, name, image, description);
        this.ingredientList = ingredientList;
        this.sourceURL = sourceURL;
    }

    public Recipe(String author) {
        super(author);
        ingredientList = new TreeMap<Ingredient, Float>();
    }

    public Recipe(String author, String name) {
        super(author);
        this.name = name;
        ingredientList = new TreeMap<Ingredient, Float>();
    }

    public Recipe(String author, String name, String image, String description, String sourceURL) {
        super(author, name, image, description);
        this.sourceURL = sourceURL;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Recipe)) return false;
        if (!super.equals(o)) return false;

        Recipe recipe = (Recipe) o;

        if (ingredientList != null && recipe.ingredientList != null && ingredientList.keySet().size() == recipe.ingredientList.keySet().size()) {
            for (Ingredient ingredient : ingredientList.keySet()) {
                if (!recipe.ingredientList.containsKey(ingredient) || !recipe.ingredientList.get(ingredient).equals(ingredientList.get(ingredient))) {
                    return false;
                }
            }
        } else {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        if (ingredientList != null) {
            int hash = 0;
            for (Ingredient ingredient : ingredientList.keySet()) {
                hash += ingredient.hashCode();
            }
            result = 31 * result + hash;
        }
        return result;
    }

    public Map<Ingredient, Float> getIngredientList() {
        return ingredientList;
    }

    public void setIngredientList(Map<Ingredient, Float> ingredientList) {
        this.ingredientList = ingredientList;
    }

    public String getSourceURL() {
        return sourceURL;
    }

    public void setSourceURL(String sourceURL) {
        this.sourceURL = sourceURL;
    }

    public JSONObject toJSONObject() {

        JSONObject json = super.toJSONObject();
        try {
            if (json != null) {
                json = new JSONObject();
            }
            json.put(Recipe.SOURCE_URL, sourceURL);
            if (ingredientList != null) {
                JSONArray array = new JSONArray();

                for (Ingredient ingredient : ingredientList.keySet()) {
                    array.put(ingredient.toJSONObject());
                }

                json.put(Recipe.INGREDIENT_LIST, array);
            }

        } catch (Exception e) {
            Log.d("Convert error", "Cannot convert Recipe to JSON", e);
        }

        return json;
    }

    public void toContentValue(ContentValues values, boolean forInserting) {
        super.toContentValue(values, forInserting);
        values.put(SOURCE_URL, getSourceURL());
        StringBuilder sb = new StringBuilder();

        for(Map.Entry<Ingredient, Float> entry : ingredientList.entrySet()){
            sb.append(entry.getKey().getId());
            sb.append("-");
            sb.append(entry.getValue());
            sb.append(BaseEntity.entitySplitSign);
        }

        values.put(INGREDIENT_LIST, sb.toString());
    }

    @Override
    public String getTableName() {
        return DataBaseHelper.RECIPE_TABLE;
    }
}
